package com.newjiabo.ski.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.newjiabo.common.constant.UserConstants;
import com.newjiabo.common.core.domain.entity.SkiMember;
import com.newjiabo.common.core.enums.ResultCode;
import com.newjiabo.common.exception.ServiceException;
import com.newjiabo.common.utils.DateUtils;
import com.newjiabo.common.utils.SecurityUtils;
import com.newjiabo.common.utils.SnowFlake;
import com.newjiabo.common.utils.StringUtils;
import com.newjiabo.ski.domain.SkiCameraman;
import com.newjiabo.ski.domain.enums.CommonAuditStatusEnum;
import com.newjiabo.ski.service.ISkiCameramanService;
import com.newjiabo.ski.service.ISkiMemberService;
import com.sun.javafx.font.directwrite.RECT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.newjiabo.ski.mapper.SkiCameramanRequestNoteMapper;
import com.newjiabo.ski.domain.SkiCameramanRequestNote;
import com.newjiabo.ski.service.ISkiCameramanRequestNoteService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 摄影师申请单
 * Service业务层处理
 *
 * @author CaoMengHui
 * @date 2023-10-18
 */
@Service
public class SkiCameramanRequestNoteServiceImpl implements ISkiCameramanRequestNoteService {
    @Resource
    private SkiCameramanRequestNoteMapper skiCameramanRequestNoteMapper;
    @Resource
    private ISkiMemberService memberService;
    @Resource
    private ISkiCameramanService cameramanService;

    /**
     * 查询摄影师申请单
     *
     * @param id 摄影师申请单主键
     * @return 摄影师申请单
     */
    @Override
    public SkiCameramanRequestNote selectSkiCameramanRequestNoteById(String id) {
        return skiCameramanRequestNoteMapper.selectSkiCameramanRequestNoteById(id);
    }

    /**
     * 查询摄影师申请单
     * 列表
     *
     * @param skiCameramanRequestNote 摄影师申请单
     * @return 摄影师申请单
     */
    @Override
    public List<SkiCameramanRequestNote> selectSkiCameramanRequestNoteList(SkiCameramanRequestNote skiCameramanRequestNote) {
        return skiCameramanRequestNoteMapper.selectSkiCameramanRequestNoteList(skiCameramanRequestNote);
    }

    /**
     * 新增摄影师申请单
     *
     * @param skiCameramanRequestNote 摄影师申请单
     * @return 结果
     */
    @Override
    public int insertSkiCameramanRequestNote(SkiCameramanRequestNote skiCameramanRequestNote) {
        if(StringUtils.isEmpty(skiCameramanRequestNote.getId())){
            skiCameramanRequestNote.setId(SnowFlake.getIdStr());
        }
        skiCameramanRequestNote.setCreateTime(DateUtils.getNowDate());
        return skiCameramanRequestNoteMapper.insertSkiCameramanRequestNote(skiCameramanRequestNote);
    }

    /**
     * 修改摄影师申请单
     *
     * @param skiCameramanRequestNote 摄影师申请单
     * @return 结果
     */
    @Override
    public int updateSkiCameramanRequestNote(SkiCameramanRequestNote skiCameramanRequestNote) {
        skiCameramanRequestNote.setUpdateTime(DateUtils.getNowDate());
        return skiCameramanRequestNoteMapper.updateSkiCameramanRequestNote(skiCameramanRequestNote);
    }

    /**
     * 批量删除摄影师申请单
     *
     * @param ids 需要删除的摄影师申请单主键
     * @return 结果
     */
    @Override
    public int deleteSkiCameramanRequestNoteByIds(String[] ids) {
        return skiCameramanRequestNoteMapper.deleteSkiCameramanRequestNoteByIds(ids);
    }

    /**
     * 删除摄影师申请单信息
     *
     * @param id 摄影师申请单主键
     * @return 结果
     */
    @Override
    public int deleteSkiCameramanRequestNoteById(String id) {
        return skiCameramanRequestNoteMapper.deleteSkiCameramanRequestNoteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int audit(String noteId, String auditStatus, String auditorRemarks) {
        SkiCameramanRequestNote skiCameramanRequestNote = this.selectSkiCameramanRequestNoteById(noteId);
        SkiMember skiMember = memberService.selectSkiMemberById(SecurityUtils.getMemberId());
        this.checkAuditMessage(skiCameramanRequestNote, skiMember);
        skiCameramanRequestNote.setAuditStatus(auditStatus);
        if (StringUtils.isNotEmpty(auditorRemarks)) {
            skiCameramanRequestNote.setAuditorRemarks(auditorRemarks);
        }
        switch (CommonAuditStatusEnum.valueOf(auditStatus)) {
            case agree:
                // 先注册摄影师信息
                String cameramanId = SnowFlake.getIdStr();
                SkiCameraman skiCameraman = BeanUtil.copyProperties(skiCameramanRequestNote, SkiCameraman.class);
                skiCameraman.setId(cameramanId);
                skiCameraman.setMemberId(skiMember.getId());
                skiCameraman.setRequestNoteId(noteId);
                cameramanService.insertSkiCameraman(skiCameraman);
                // 更改会员信息
                skiMember.setHaveCameraman(true);
                skiMember.setCameramanId(cameramanId);
                memberService.updateSkiMember(skiMember);
                break;
            case refund:
                break;
        }
        return this.updateSkiCameramanRequestNote(skiCameramanRequestNote);
    }

    @Override
    public int apply(SkiCameramanRequestNote cameramanRequestNote) {
        // 检查当前用户是否存在申请中的摄影师
        String memberId = SecurityUtils.getMemberId();
        SkiCameramanRequestNote query = new SkiCameramanRequestNote();
        query.setMemberId(memberId);
        List<SkiCameramanRequestNote> skiCameramanRequestNotes = this.selectSkiCameramanRequestNoteList(query);
        for (SkiCameramanRequestNote skiCameramanRequestNote : skiCameramanRequestNotes) {
            if(CommonAuditStatusEnum.apply.name().equals(skiCameramanRequestNote.getAuditStatus())){
                throw new ServiceException(ResultCode.THE_PHOTOGRAPHER_IS_CURRENTLY_APPLYING);
            }
            if(CommonAuditStatusEnum.agree.name().equals(skiCameramanRequestNote.getAuditStatus())){
                throw new ServiceException(ResultCode.REGISTERED_PHOTOGRAPHER);
            }
        }
        this.initApply(cameramanRequestNote);
        if(StringUtils.isEmpty(cameramanRequestNote.getMemberId())){
            cameramanRequestNote.setMemberId(SecurityUtils.getMemberId());
        }
        return this.insertSkiCameramanRequestNote(cameramanRequestNote);
    }

    private void initApply(SkiCameramanRequestNote cameramanRequestNote){
        cameramanRequestNote.setAuditStatus(CommonAuditStatusEnum.apply.name());
    }

    public void checkAuditMessage(SkiCameramanRequestNote skiCameramanRequestNote, SkiMember skiMember) {
        if (!CommonAuditStatusEnum.apply.name().equals(skiCameramanRequestNote.getAuditStatus())) {
            throw new ServiceException(ResultCode.STATUS_FOR_REVIEW);
        }
        if (skiMember.getHaveCameraman()) {
            throw new ServiceException(ResultCode.REGISTERED_PHOTOGRAPHER);
        }
    }
}
